﻿using System.ServiceProcess;
using System.Threading;
using System;

namespace PSE.Deployment.Util
{
    public static class WindowsServiceController
    {
        public static bool Start(string serviceName)
        {
            return Start(serviceName, null);
        }

        public static bool Start(string serviceName, string[] args)
        {
            bool started = false;

            WindowsService.ServiceInfo serviceInfo = WindowsService.GetServiceInfo(serviceName);

            if (serviceInfo != null)
            {
                using (ServiceController controller = new ServiceController(serviceName))
                {
                    if (controller.Status == ServiceControllerStatus.Stopped)
                    {
                        if (args != null)
                        {
                            controller.Start(args);
                        }
                        else
                        {
                            controller.Start();
                        }

                        controller.Refresh();

                        while (controller.Status != System.ServiceProcess.ServiceControllerStatus.Running)
                        {
                            Thread.Sleep(250);
                            controller.Refresh();

                            if (controller.Status == ServiceControllerStatus.Stopped)
                                break;
                        }

                        started = (controller.Status == System.ServiceProcess.ServiceControllerStatus.Running);
                    }
                    else//caso ja esteja rodando, marcar como iniciado para nao ocorrer erros
                        started = true;
                }
            }

            return started;
        }

        public static bool Stop(string serviceName)
        {
            bool stopped = false;

            WindowsService.ServiceInfo serviceInfo = WindowsService.GetServiceInfo(serviceName);

            if (serviceInfo != null)
            {
                using (ServiceController controller = new ServiceController(serviceName))
                {
                    if (controller.Status == ServiceControllerStatus.Running)
                    {
                        controller.Stop();

                        controller.Refresh();

                        controller.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Stopped);

                        stopped = true;
                    }
                    else//caso n esteja rodando, marcar como finalizado para nao ocorrer erros
                        stopped = true;
                }
            }

            return stopped;
        }

        public static ServiceControllerStatus? GetServiceStatus(string serviceName)
        {
            ServiceControllerStatus? status = null;

            WindowsService.ServiceInfo serviceInfo = WindowsService.GetServiceInfo(serviceName);

            if (serviceInfo != null)
            {
                using (ServiceController controller = new ServiceController(serviceName))
                {
                    status = controller.Status;
                }
            }

            return status;
        }
    }
}
